package com.hschinese.pronunciation.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolUtil {
	private static ThreadPoolUtil threadPoolUtil = null;
	private static final int POOL_SIZE = 3;
	private ExecutorService pool;

	private ThreadPoolUtil(){
		pool = Executors.newFixedThreadPool(POOL_SIZE);
	}

	public static ThreadPoolUtil getInstance(){
		if(null == threadPoolUtil){
			threadPoolUtil = new ThreadPoolUtil();
		}
		return threadPoolUtil;
	}

	public void execute(Runnable run){
		pool.execute(run);
	}

	public void shutdown(){
		if(pool.isShutdown()){
			return ;
		}
		pool.shutdown();
	}

	public void shutdownNow(){
		pool.shutdownNow();
		pool = null;
		threadPoolUtil= null;
	}
	//	private static ThreadPoolUtil threadPoolUtil = null;
	//	private static final int POOL_SIZE = 3;
	//	private ThreadPoolExecutor pool;
	//
	//	private ThreadPoolUtil(){
	//		pool = new ThreadPoolExecutor(0, POOL_SIZE,
	//                30L, TimeUnit.SECONDS,
	//                new SynchronousQueue<Runnable>());
	//	}

	//	public static ThreadPoolUtil getInstance(){
	//		if(null == threadPoolUtil){
	//			threadPoolUtil = new ThreadPoolUtil();
	//		}
	//		return threadPoolUtil;
	//	}
	//
	//	public ExecutorService getPool() {
	//		return pool;
	//	}
	//
	//	public void execute(Runnable run){
	//		if(pool.isShutdown()){
	//			
	//		}
	//		pool.execute(run);
	//	}
	//	
	//	public void shutdown(){
	//		if(pool.isShutdown()){
	//			threadPoolUtil = new ThreadPoolUtil();
	//		}
	//		pool.shutdown();
	//	}
	//
	//	public void shutdownNow(){
	//		if(pool.isShutdown()){
	//			return ;
	//		}
	//		pool.shutdownNow();
	//		pool = null;
	//		threadPoolUtil= null;
	//	}

}
